﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using UtilsORM;
using UtilsORM.MySqlData;

////using System.Web.Script.Serialization;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceTaskTimeInterval
    {


        EFYihongIotContext ef = new EFYihongIotContext();
        public iot_device_task_time_interval getModel(string guid)
        {
            return ef.iot_device_task_time_interval.Where(c => c.ti_guid == guid).FirstOrDefault();
        }

        public List<iot_device_task_time_interval> getTimeIntervalByPguid(string pguid)
        {
            return ef.iot_device_task_time_interval.Where(c => c.ti_p_guid == pguid).ToList();
        }


        public bool saveAll(IFormCollection FC, HttpReqModel httpRequest, MagerLoginInfo LoginUserInfo, iot_device_task_time_interval req)
        {

            //先查一下有没重复提交
            iot_device_task_time_interval model = ef.iot_device_task_time_interval.Where(c => c.ti_guid == req.ti_guid).FirstOrDefault();
            if (model == null)
            {

                //写入
                req.ti_add_time = DateTime.Now;
                req.ti_mod_time = DateTime.Now;
                req.ti_start_time = DateTime.Now;
                req.ti_interval_name = "秒";
                req.ti_interval_unit = "ss";
                req.ti_is_template = 0;
                req.ti_auto_run = 0;
                req.ti_auto_run_jion = 0;
                req.ti_execute_time= DateTime.Parse("2000-01-01 00:00:00");
                // req.ti_execute_count = 0;
                req.ti_c_guid = LoginUserInfo.userCompanyGuid;
                if (!string.IsNullOrEmpty(req.ti_p_guid))
                {
                    req.ti_is_template = 1;
                    iot_device_task_time_interval getmodel = ef.iot_device_task_time_interval.Where(c => c.ti_p_guid == req.ti_p_guid && c.ti_p_model_no!=null).OrderByDescending(c => c.ti_p_model_no).FirstOrDefault();
                    if (getmodel != null && getmodel.ti_p_model_no != null)
                    {
                        req.ti_p_model_no = getmodel.ti_p_model_no + 1;
                    }
                    else
                    {
                        req.ti_p_model_no = 1;
                    }

                    req.ti_c_guid = ef.iot_product.Where(c => c.p_guid == req.ti_p_guid).FirstOrDefault().p_c_guid;
                }

                ef.iot_device_task_time_interval.Add(req);
                ef.SaveChanges();
            }
            else
            {
                model.ti_title = req.ti_title;
                model.ti_status = req.ti_status;
                model.ti_interval_value = req.ti_interval_value;
                model.ti_key = req.ti_key;
                model.ti_task_guid = req.ti_task_guid;
                //model.ti_repeat_count = req.ti_repeat_count;
                model.ti_mod_time = DateTime.Now;

                update(model);
            }

            return true;
        }
        static object updobj = new object();
        public void update(iot_device_task_time_interval model)
        {
            lock (updobj)
            {
                ef.iot_device_task_time_interval.Update(model);
                ef.SaveChanges();
            }

        }


        public void del(string tiguid)
        {
            lock (updobj)
            {
                ef.iot_device_task_time_interval.Where(c=>c.ti_guid==tiguid).ExecuteDelete();
                ef.SaveChanges();
            }

        }

        public bool changeStatus(string ti_guid)
        {
            iot_device_task_time_interval model = ef.iot_device_task_time_interval.Where(c => c.ti_guid == ti_guid).FirstOrDefault();
            if (model != null)
            {
                if (model.ti_status == 0)
                {
                    model.ti_status = 1;
                }
                else
                {
                    model.ti_status = 0;
                }
                ef.SaveChanges();
            }
            return true;
        }
    }
}
